﻿using UnityMethods.Extend;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using UnityTemplate.API.LogHelper;
using UnityTemplate.ApiModel;
using UnityTemplate.ApiModel.Models;
using UnityTemplate.ApiModel.Request;
using UnityTemplate.ApiModel.Response;
using UnityTemplate.Repository;
using UnityTemplate.Services.Interface;

namespace UnityTemplate.API.Controllers.v1
{    
    public class UserController : BaseApiController
    {
        /// <summary>
        /// Jwt 服务
        /// </summary>
        private readonly IJwtAuthorizeService _jwtAuthorize;

        public UserController(
            ILoggerHelper logger,
            IJwtAuthorizeService jwtApp,
            IUserService userService,
            IOptionsMonitor<JwtConfig> optionsMonitor)
            : base(userService, logger)
        {
            _jwtAuthorize = jwtApp;
            _jwtConfig = optionsMonitor.CurrentValue;
        }

        #region 登录

        private readonly JwtConfig _jwtConfig;

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UserLogin")]
        [AllowAnonymous]
        public ActionResult<BaseResponse> UserLogin(LoginRequest login)
        {
            BaseUserInfos user = UserService.GetUserInfoByPassword(login.UserName, login.Password);
            if (user != null && !user.Id.IsEmpty())
            {
                LoginResponse response = new LoginResponse()
                {
                    Token = _jwtAuthorize.CreateToken(new IdentityUser()
                    {
                        UserName = login.UserName,
                        Id = user.Id.ToString(),
                        PhoneNumber = user.MobilePhone
                    }, _jwtConfig.SecurityKey),
                    User = Ext.TransCopyData<BaseUserInfos, UserInfos>(user)
                };

                return BaseResponse.OK(response);
            }
            else
            {
                return BaseResponse.BadRequest("用户名或密码错误。", ApiStatus.ERROR);
            }

        }

        #endregion

        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("UserInfo")]
        [Authorize(Roles = "Client")]
        public ActionResult<BaseResponse> UserInfo()
        {
            return BaseResponse.OK(UserService.GetUserInfo(UserId, UserName));
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        [Route("Register")]
        public ActionResult<BaseResponse> RegisteUser([FromBody] RegisterUserRequest registerUser)
        {
            int status = UserService.AddUser(Ext.TransCopyData<UserInfos, BaseUserInfos>(registerUser.UserInfos));

            switch (status)
            {
                case ApiStatus.FailedToRegister:
                    return BaseResponse.BadRequest("新增用户失败", ApiStatus.FailedToRegister);
                case ApiStatus.UserExist:
                    return BaseResponse.BadRequest("用户已存在", ApiStatus.UserExist);
            }

            return BaseResponse.OK("新增用户成功");
        }
    }
}
